package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.*;
import com.qf.pojo.*;
import com.qf.service.DtsGoodsAttributeService;
import com.qf.service.DtsGoodsProductService;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsGoodsSpecificationService;
import com.qf.util.AdminResponseUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {
@Autowired
    DtsCategoryMapper categoryMapper;
@Autowired
DtsBrandMapper brandMapper;

    @Autowired
    DtsGoodsProductMapper goodsProductMapper;
    @Autowired
    DtsGoodsSpecificationMapper goodsSpecificationMapper;

    @Autowired
    DtsGoodsAttributeMapper goodsattributeMapper;

    @Autowired
    DtsGoodsMapper goodsMapper;

    @Autowired
    DtsUserMapper userMapper;
    @Override
    public Integer getTotal() {
        return userMapper.selectCount(null);
    }

    @Override
    public IPage<DtsGoods> queryPage(Integer page, Integer limit, String sort, String order, String goodsSn, String name, List<Integer> brandIds) {
        IPage<DtsGoods> ipage = new Page<>(page,limit);
        QueryWrapper<DtsGoods> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if(order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        if(!StringUtils.isEmpty(goodsSn)) {
            qw.like("goods_sn", goodsSn);
        }
        if(!StringUtils.isEmpty(name)) {
            qw.like("name", name);
        }
        qw.eq("deleted",0);
        if(!CollectionUtils.isEmpty(brandIds)) {
            qw.in("brand_id", brandIds);
        }
        goodsMapper.selectPage(ipage, qw);
        return ipage;
    }

    @Override
    public Map<String, Object> queryById(Integer id) {
        DtsGoods goods = goodsMapper.selectById(id);
        LambdaQueryWrapper<DtsGoodsAttribute> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsAttribute::getGoodsId , id);
        qw.eq(DtsGoodsAttribute::getDeleted, false);
        List<DtsGoodsAttribute> goodsAttributes = goodsattributeMapper.selectList(qw);
        LambdaQueryWrapper<DtsGoodsProduct> qw1= new LambdaQueryWrapper<>();
        qw1.eq(DtsGoodsProduct::getGoodsId , id);
        qw1.eq(DtsGoodsProduct::getDeleted, false);
        List<DtsGoodsProduct> dtsGoodsProducts = goodsProductMapper.selectList(qw1);

        LambdaQueryWrapper<DtsGoodsSpecification> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(DtsGoodsSpecification::getGoodsId , id);
        qw2.eq(DtsGoodsSpecification::getDeleted, false);

        List<DtsGoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectList(qw2);

        Integer categoryId = goods.getCategoryId();
        DtsCategory category = categoryMapper.selectById(categoryId);
        Integer pid = category.getPid();
        Map<String,Object> data = new HashMap<>();
        data.put("categoryIds", Arrays.asList(pid, categoryId) );
        data.put("goods", goods);
        data.put("attributes", goodsAttributes);
        data.put("specifications", goodsSpecifications);
        data.put("products", dtsGoodsProducts) ;
        return data;
    }


    @Autowired
    DtsGoodsProductService goodsproductService;

    @Autowired
    DtsGoodsSpecificationService goodsspecificationService;

    @Autowired
    DtsGoodsAttributeService goodsattributeService;

    @Override
    public Object update(GoodsAllinone goodsAllinone) {
        Object result = this.check(goodsAllinone);
        if(result != null) {
            return result;
        }
        DtsGoods goods = goodsAllinone.getGoods();
        goods.setUpdateTime(new Date());
        goodsMapper.updateById(goods);
        DtsGoodsProduct[] goodsproducts = goodsAllinone.getProducts();
        goodsproductService.updateProducts(goods.getId(),goodsproducts );
        DtsGoodsSpecification[] goodsspecifications = goodsAllinone.getSpecifications();
        goodsspecificationService.updateSpecifications(goods.getId(),goodsspecifications);
        DtsGoodsAttribute[] goodsattributes = goodsAllinone.getAttributes();
        goodsattributeService.updateAttributes(goods.getId(), goodsattributes);

        return ResponseUtil.ok();
    }

    @Override
    public Object delete(DtsGoods goods) {
        long count = goodsMapper.selectOrderCountById(goods.getId());
        if(count > 0) {
            return ResponseUtil.fail(-1, "还有商品在售卖");
        }
        goods.setDeleted(true);
        goodsMapper.updateById(goods);
        goodsattributeService.deleteById(goods.getId());
        goodsspecificationService.deleteById(goods.getId());
        goodsproductService.deleteById(goods.getId());

        return ResponseUtil.ok();
    }

    @Override
    public Object create(GoodsAllinone goodsAllinone) {
        Object result = this.check(goodsAllinone);
        if(result != null) {
            return result;
        }
        DtsGoods goods = goodsAllinone.getGoods();
        LambdaQueryWrapper<DtsGoods> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoods::getName, goods.getName());
        Integer count = goodsMapper.selectCount(qw);
        if(count>0) {
            return ResponseUtil.fail(-1, "商品名已有");
        }
        goods.setUpdateTime(new Date());
        goods.setAddTime(new Date());
        goods.setDeleted(false);
        goodsMapper.insert(goods);
        goodsattributeService.addAttribute(goods.getId(),goodsAllinone.getAttributes());
        goodsproductService.addProduct(goods.getId(),goodsAllinone.getProducts());
        goodsspecificationService.addSpecification(goods.getId(),goodsAllinone.getSpecifications());
        return ResponseUtil.ok();
    }

    private Object check(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        if(goods == null){
            return ResponseUtil.badArgument();
        }

        if(StringUtils.isEmpty(goods.getGoodsSn())) {
            return AdminResponseUtil.fail(-1, "商品编号不能为空");
        }
        if(StringUtils.isEmpty(goods.getName())) {
            return AdminResponseUtil.fail(-1, "商品名不能为空");
        }


        Integer brandId = goods.getBrandId();
        DtsBrand brand = brandMapper.selectById(brandId);
        if(brand == null) {
            return AdminResponseUtil.fail(-1, "品牌商不存在");
        }

        Integer categoryId = goods.getCategoryId();
        DtsCategory category = categoryMapper.selectById(categoryId);
        if(category == null) {
            return AdminResponseUtil.fail(-1, "分类不存在");
        }
        DtsGoodsAttribute[] goodsattributes = goodsAllinone.getAttributes();
        if(goodsattributes == null&&goodsattributes.length == 0) {
            return AdminResponseUtil.fail(-1, "商品属性不能为空");
        }

        DtsGoodsProduct[] goodsproducts = goodsAllinone.getProducts();
        if(goodsproducts == null&&goodsproducts.length == 0) {
            return AdminResponseUtil.fail(-1, "商品库存不能为空");
        }
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if(specifications == null&&specifications.length == 0) {
            return AdminResponseUtil.fail(-1, "商品规格不能为空");
        }
        return null;

    }
}
